package com.jintoufs.web.action.quartz;

import com.jintoufs.domain.baseinfo.CurrencyExchange;
import com.jintoufs.service.baseinfo.CurrencyExchangeService;
import com.jintoufs.util.http.CurrencyApi;
import com.jintoufs.web.action.support.SupportAction;

import javax.annotation.Resource;

/**
 * Created by xiaoemo on 2017/11/23.
 * @author xiaoemo
 */
public class CurrencyRateQuartzAction extends SupportAction {
    @Resource
    private CurrencyExchangeService currencyExchangeService;
    private String[] SCUR = {"CNY", "USD","EUR","JPY","GBP","AUD","HKD","CAD","FRF","KRW","RUR"};
    private String[] TCUR = {"CNY", "USD","EUR","JPY","GBP","AUD","HKD","CAD","FRF","KRW","RUR"};

    /**
     * 定时获取API数据任务
     */
    public void rateTask() throws Exception {
        for (String scur : SCUR) {
            for (String tcur : TCUR) {
                if (!scur.equalsIgnoreCase(tcur)) {
                    double rate = CurrencyApi.getCurrentRate(scur, tcur);
                    if(rate>0){
                        CurrencyExchange currencyExchange = new CurrencyExchange();
                        currencyExchange.setRate(rate);
                        currencyExchange.setScur(scur);
                        currencyExchange.setTcur(tcur);
                        currencyExchangeService.insert(currencyExchange);
                    }
                }
            }
        }
    }
}
